<?php 
class Manager_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }
    function getParent($manager){
        $sql = "SELECT parent FROM tblmanager WHERE id='$manager'";
        $arr = $this->db->query($sql)->result_array();
        if(count($arr)>0)
            return $arr[0]['parent'];
        else return '0';
    }
    function get_onwer($pj_id){//lay quan ly cua nhan vien hien hanh trong du an
        $pn_id = auth_id();
        //$sql = "SELECT * FROM `tblmanager` WHERE projectid='$pj_id' AND parent IN (SELECT id FROM tblmanager WHERE personnelid='$pn_id')";
        $sql = "SELECT tblmanager.id,tblpersonnels.name as 'personnelname',tblworks.name as 'workname',tblprojects.name as 'projectname', 
            case tblmanager.status 
            	when '0' then 'Chưa cho phép phân công'
                when '1' then 'Được phép phân công'
                when '2' then 'Đã phân công'
            end as 'status'
            FROM tblmanager 
            JOIN tblpersonnels ON tblmanager.personnelid=tblpersonnels.id 
            JOIN tblworks ON tblmanager.workid=tblworks.id 
            JOIN tblprojects ON tblmanager.projectid=tblprojects.id
            WHERE tblmanager.projectid = '$pj_id' AND tblmanager.bossid IS NULL
            AND parent IN (SELECT id FROM tblmanager WHERE personnelid='$pn_id'
            AND tblmanager.status ='1')
         ";
        return $this->db->query($sql)->result_array();
    } 
    function get_of_work($workid){
        $sql = "SELECT id FROM tblmanager WHERE workid='$workid'";
        $arr = $this->db->query($sql)->result_array();
        if(count($arr)>0)
            return $arr[0]['id'];
        else return '0';
    }
    function get_lever_1($pj_id){// lay phan cong cap 1 - pc cua truong du an gom truong nhom va cong viec
        $sql = "SELECT tblmanager.id,tblpersonnels.name as 'personnelname',tblworks.name as 'workname',tblprojects.name as 'projectname', 
            case tblmanager.status 
            	when '0' then 'Chưa cho phép phân công'
                when '1' then 'Được phép phân công'
                when '2' then 'Đã phân công'
            end as 'status'
            FROM tblmanager 
            JOIN tblpersonnels ON tblmanager.personnelid=tblpersonnels.id 
            JOIN tblworks ON tblmanager.workid=tblworks.id 
            JOIN tblprojects ON tblmanager.projectid=tblprojects.id
            WHERE tblmanager.projectid = '$pj_id' AND tblmanager.bossid IS NULL
             AND tblmanager.status ='1'
         ";
        return $this->db->query($sql)->result_array();
    }
    function get_managers($pj_id){
    	// Hàm này lấy quản lý tùy theo admin|pm hoặc là trưởng nhóm
    	if(is_admin()||is_pm($pj_id)){
    		$sql = "SELECT tblmanager.id,tblpersonnels.name as personnelname, tblworks.name as workname 
               FROM tblmanager 
               JOIN tblpersonnels ON tblpersonnels.id = tblmanager.personnelid 
               JOIN tblworks ON tblworks.id = tblmanager.workid 
               WHERE tblmanager.projectid = '$pj_id'
               AND tblmanager.status !='2'";
    	}else {
    		$pn_id = auth_id();
    		$sql = "SELECT tblmanager.id,tblpersonnels.name as personnelname, tblworks.name as workname 
               FROM tblmanager 
               JOIN tblpersonnels ON tblpersonnels.id = tblmanager.personnelid 
               JOIN tblworks ON tblworks.id = tblmanager.workid 
               WHERE tblmanager.projectid = '$pj_id' 
               AND tblmanager.parent in (SELECT id FROM tblmanager WHERE personnelid='$pn_id' AND projectid='$pj_id')
               AND tblmanager.status !='2'";
    	}
        
        $arr = $this->db->query($sql)->result_array();
        for($i=0;$i<count($arr);$i++){
            $arr[$i]['managername'] = $arr[$i]['personnelname'] .' - Công việc '.$arr[$i]['workname'];
            unset($arr[$i]['personnelname']);
            unset($arr[$i]['workname']);
        }
        return $arr;
    }
    function get_personnelid($managerid){
        $arr = $this->db->query("SELECT personnelid FROM tblmanager WHERE id='$managerid'")->result_array();
		if(count($arr)>0)
        	return $arr[0]['personnelid'];
		else return 0;
    }
	  function get_workid($managerid){
        $arr = $this->db->query("SELECT workid FROM tblmanager WHERE id='$managerid'")->result_array();
        return $arr[0]['workid'];
    }
    function delete($id){
        $this->db->query("DELETE FROM tblmanager WHERE id='$id' or parent='$id'");
        $this->db->query("DELETE FROM tblassigns WHERE managerid='$id'");
    }
    function get_manager_without($parent_maid){
        $sql = "SELECT tblmanager.id FROM tblmanager  WHERE tblmanager.parent = '$parent_maid'";
        $arr = $this->db->query($sql)->result_array();
        $str = '';
        foreach ($arr as $v) {
            $str .= $v['id'].'-';
        }
        if($str!=''){
            $str = substr($str,0,-1);
        }
        return $str;
    }
}
?>
